#!/usr/bin/python
"""
Initialize db.
"""

import pg
from optparse import OptionParser

schemas = [
    'init/gotbattles/logs.sql',
    'init/gotbattles/users.sql',
    'init/gotbattles/auth_tokens.sql',
    'init/gotbattles/settings.sql',
    'init/gotbattles/profile_settings.sql',
    'init/games/tactics/matches.sql',
    'init/games/tactics/match_statuses.sql',
    'init/games/tactics/players.sql',
    'init/games/tactics/maps.sql',
    'init/games/tactics/pawns.sql'
]

testData = [
    'test/gotbattles/users.sql',
    'test/gotbattles/auth_tokens.sql']

def getSql (filename):
    print 'Getting SQL from: ', filename

    f = open(filename, 'r')
    return f.read()
    f.close()

def executeSql (user, passwd, database, sql):
    print 'Executing SQL:'
    print sql

    connection = pg.connect(database, 'localhost', user = user, passwd = passwd)
    connection.query(sql)

    print 'successfully executed'
    print

def clearTables (user, password, database):
    print 'Removing tables'

def createTables (user, password, database):
    print 'Initializing GotBattles tables.'
    for schema in schemas:
        sql = getSql(schema)
        executeSql(user, password, database, sql)

def initTestData (user, password, database):
    print 'Initializing GotBattles test data.'
    for schema in testData: 
        sql = getSql(schema)
        executeSql(user, password, database, sql)

def initDatabase (user, password, database):
    createTables(user, password, database)

def main():
    # TODO(rhawkins): This script should manage completely creating and setting up the database.
    #     Some of the features selectable from the command line should be as follows:
    #     1. Initialize App (Bare database, no user data).
    #     2. Insert testing user data (same as #5, but instead of using backup files, the pre
    #         supplied 'test' data is used.
    #     3. Clean database (remove it, and re-init it).
    #     4. Backup database (Backup all the user data).
    #     5. Restore backup (Restore from backedup files).

    parser = OptionParser()
    # Default value options
    parser.add_option("--user", default="gb_testing")
    parser.add_option("--password", default="swePe&&ut4eD")
    parser.add_option("--database", default="gb_testing")
    # Run Mode Options
    parser.add_option("--initAll", action="store_true", default=False)
    parser.add_option("--initDatabase", action="store_true", default=False)
    parser.add_option("--initTestData", action="store_true", default=False)
    (options, args) = parser.parse_args()

    if (options.initAll):
        options.initDatabase = True
        options.initTestData = True

    # TODO(rhawkins): if no mode is specified, this should instruct the user to use the help.

    if (options.initDatabase):
        initDatabase(options.user, options.password, options.database)

    if (options.initTestData):
        initTestData(options.user, options.password, options.database)

if (__name__ == '__main__'):
    main()
